package com.aplink.sg.mobilespiff.activities;

import com.aplink.generic.GenericActivity;
import com.aplink.generic.cache.ContentDataManager;
import com.aplink.generic.database.DatabaseHelper;
import com.aplink.generic.interfaces.OnControlSubmitListener;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.webservice.GenericWebService;
import com.aplink.sg.mobilespiff.R;

public class ProofOfSaleDetailsActivity extends GenericActivity implements
		OnControlSubmitListener {

	@Override
	protected int initContentData() {
		return R.raw.proof_of_sale_detail_contentdata;
	}

	@Override
	protected int initFormDesign() {
		return R.raw.proof_of_sale_detail_formdesign;
	}

	@Override
	public void onControlSubmit(ControlSpec controlSpec) {
		if (controlSpec.getIDValue() == R.id.ok_button) {
			String invoiceID = getGenericFragment().getInitValues().getValue(
					"Invoice.InvoiceID");
			ContentData invoiceContentData = ContentDataManager.getInstance()
					.get(R.raw.proof_of_sale_contentdata);
			GenericObject genericObject = DatabaseHelper.getInstance()
					.get(invoiceContentData,
							"Invoice.InvoiceID='" + invoiceID + "'");
			genericObject.put("Invoice.IsChange", "1");

			DatabaseHelper.getInstance().update(genericObject,
					invoiceContentData);
			if (!GenericSharePreference.getInstance().isOfflineMode()) {
				GenericWebService.newInstance(getGenericFragment()).upload()
						.excute();
			}
			DatabaseHelper.getInstance().delete("InvoiceStatus", "InvoiceID=?",
					new String[] { invoiceID });
		}
	}
}
